<?php
namespace plugins\order\controller;

use cmf\controller\ApiPluginBaseController;

class ApiShoppingTrolleyController extends ApiPluginBaseController
{

	//购物车列表
	public function getShoppingTrolley()
	{
		$post = $this->request->post();
		$user = $this->getIdentity();
		$post['order']['member_id'] = $user->uid;
		
		$check = ( new \plugins\member\model\MemberModel )->get(['uid'=>$user->uid]);
		if($check){
			//购物车的列表
			$list = ( new \plugins\order\model\OrderShoppingTrolleyModel )->field('sku,num,store_id')->where(['uid'=>$user->uid])->order('id','desc')->select()->toArray();
			$arr = [];
			foreach ($list as $key => $value) {
				$store = ( new \plugins\store\model\StoreModel )->get(['store_id'=>$value['store_id']]);
				if($store == null){
					continue;
				}
				//查找店铺id作为key
				$arr[$value['store_id']]['store_name'] = $store->store_name;
				$arr[$value['store_id']]['store_id'] = $value['store_id'];

				$info['sku'] = $value['sku'];
				$info['num'] = $value['num'];
				$info['goods_name'] = '';
				$info['goods_price'] = '';
				$info['spec_name'] = '';
				$info['goods_id'] = '';
				$info['goods_img'] = '';
				//sku搜索商品信息
				$goods_spec = ( new \plugins\goods\model\GoodsSpecListModel )->get(['sku'=>$value['sku']]);
				//商品信息有效
				if(!empty($goods_spec)){
					$good = ( new \plugins\goods\model\GoodsModel )->get(['id'=>$goods_spec->gid]);
					if($good == null){
						$info['status'] = false;
						$arr[$value['store_id']]['info'][] = $info;
						continue;
					}
					$info['status'] = true;
					//商品id
					$info['goods_id'] = ( new \plugins\goods\model\GoodsModel )->get(['id'=>$goods_spec->gid])->id;
					//中文名
					$info['goods_name'] = ( new \plugins\goods\model\GoodsModel )->get(['id'=>$goods_spec->gid])->goods_name;
					//价格
					$info['goods_price'] = $goods_spec->price;
					//商品图片
					$info['goods_img'] = zy_domain().'/plugins/goods/view/image/'.$goods_spec->img;
					//规格转中文
					$spec = explode('-', $goods_spec->attr);
					foreach ($spec as $k => $v) {
						$info['spec_name'] .= ( new \plugins\goods\model\GoodsSpecAttrModel )->get(['attr_id'=>$v])->attr_name.',';
					}
					rtrim($info['spec_name'],',');
				}
				$arr[$value['store_id']]['info'][] = $info;
			}
			return api_json_echo(true,'查询完成',$arr);
		}

		return api_json_echo(false,'不存在的用户');

	}

	//添加商品
	public function addShoppingTrolley()
	{
		$post = $this->request->post();
		$info = $this->getIdentity();

		//uid num sku
		$check = ( new \plugins\member\model\MemberModel )->get(['uid'=>$info->uid]);
		if($check){
			$post['uid'] = $info->uid;
			//sku检查
			//检查sku有效性
			$sku_check = ( new \plugins\goods\model\GoodsSpecListModel )->get(['sku'=>$post['sku']]);
			if(!$sku_check){
				return api_json_echo(false,'不存在的商品');
			}
			//检查购物车中是否已有商品
			$car_check = ( new \plugins\order\model\OrderShoppingTrolleyModel )->get(['sku'=>$post['sku'],'uid'=>$info->uid]);
			if($car_check){
				$post['store_id'] = $sku_check->store_id;
				$post['num'] += $car_check->num;
				$car_check->allowField(true)->isUpdate(true)->save($post);
				return api_json_echo(true,'添加完成');
			}

			//无记录时加新
			$result = ( new \plugins\order\model\OrderShoppingTrolleyModel )->allowField(true)->save($post);
			if($result){
				return api_json_echo(true,'添加完成');
			}

			return api_json_echo(false,'添加失败');
		}

		return api_json_echo(false,'不存在的用户');

	}

	//删除商品
	public function delShoppingTrolley()
	{
		$post = $this->request->post();
		$info = $this->getIdentity();
		$check = ( new \plugins\member\model\MemberModel )->get(['uid'=>$info->uid]);
		//传入 uid sku
		if($check){
			$result = ( new \plugins\order\model\OrderShoppingTrolleyModel )->destroy(['uid'=>$info->uid,'sku'=>$post['sku']]);
			if($result){
				return api_json_echo(true,'删除完成');
			}
			return api_json_echo(false,'删除失败');
		}

		return api_json_echo(false,'不存在的用户');		
	}

	//商品数量更改
	public function changeShoppingTrolley()
	{
		$post = $this->request->post();
		$info = $this->getIdentity();
		//uid num sku
		$check = ( new \plugins\member\model\MemberModel )->get(['uid'=>$info->uid]);
		if($check){

			//sku检查
			//检查sku有效性
			$sku_check = ( new \plugins\goods\model\GoodsSpecListModel )->get(['sku'=>$post['sku']]);
			if(!$sku_check){
				return api_json_echo(false,'不存在的sku');
			}
			//检查购物车中是否已有商品
			$car_check = ( new \plugins\order\model\OrderShoppingTrolleyModel )->get(['sku'=>$post['sku'],'uid'=>$info->uid]);
			if($car_check){
				$car_check->num = $post['num'];
				$car_check->save();
				return api_json_echo(true,'修改完成');
			}

			return api_json_echo(false,'购物车中无此商品');

		}

		return api_json_echo(false,'不存在的用户');		
	}

}